<template>
  <view>
    <u-navbar :is-back="false" title="　" :border-bottom="false"> </u-navbar>
    <view
      @click="handleClickMenu('shopDetail')"
      class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30"
    >
      <view class="u-m-r-10">
        <u-avatar :src="storeData.storeLogo" size="140"></u-avatar>
      </view>
      <view class="u-flex-1">
        <view class="u-font-18 u-p-b-20 ipad-title">{{ storeData.storeName }}</view>
        <view class="u-font-12 u-tips-color ipad-dec">
          {{ storeData.storeAddressPath + "-" + storeData.storeAddressDetail }}
        </view>
      </view>
      <view class="u-m-l-10 u-p-10">
        <u-icon name="arrow-right" color="#969799" size="28"></u-icon>
      </view>
    </view>
    <view class="u-m-t-20">
      <u-cell-group>
        <!-- <u-cell-item @click="handleClickMenu('shopAddress')" title="自提管理"/> -->
        <u-cell-item @click="handleClickMenu('logistics')" title="物流公司" />
        <u-cell-item @click="handleClickMenu('returnGoods')" title="退货地址" />
        <u-cell-item @click="handleClickMenu('stockWarning')" title="库存预警" />
        <!-- #ifdef APP-PLUS -->
        <u-cell-item
          @click="handleClickMenu('upLoad')"
          :arrow="false"
          title="检查更新"
          :value="appVersion"
        />
        <!-- #endif -->
        <!-- #ifdef APP-PLUS || MP-WEIXIN -->
        <u-cell-item
          @click="handleClickMenu('cache')"
          :arrow="false"
          title="清除缓存"
          :value="cacheSize"
        />
        <!-- #endif -->
        <u-cell-item @click="loginOutMoadl" title="退出登录" />
      </u-cell-group>
    </view>
    <u-modal
      v-model="show"
      :show-title="true"
      title="提示"
      :show-cancel-button="true"
      :content="content"
      @confirm="loginOut"
    ></u-modal>
  </view>
</template>

<script>
import { getShopInfo } from "@/api/shops";
import { loginOutAsync } from "@/api/login.js";
import { getAppVersion } from "@/api/common.js";
import { compareVersion } from "@/utils/RegExp.js";
export default {
  data() {
    return {
      show: false,
      content: "退出登录",
      storeData: {}, //店铺详情
      appVersion: "获取应用版本", // 应用版本号
      cacheSize: "正在计算缓存", // 缓存大小
      ktxWindowWidth: "",
    };
  },
  onLoad() {
    let systemInfo = uni.getSystemInfoSync();
    this.ktxWindowWidth = systemInfo.windowWidth;
    // #ifdef APP-PLUS
    this.getVersion();
    // #endif
    // #ifdef APP-PLUS || MP-WEIXIN
    this.getCache();
    // #endif
  },
  onShow() {
    this.init();
  },
  methods: {
    loginOut() {
      loginOutAsync().then((res) => {
        if (res.data.code === 200) {
          uni.showToast({
            title: "操作成功",
            icon: "none",
            duration: 500,
          });
          setTimeout(() => {
            uni.clearStorageSync();
            uni.reLaunch({
              url: "/pages/passport/login",
            });
          });
        }
      });
    },
    loginOutMoadl() {
      this.show = true;
    },
    // 初始化店铺详情
    async init() {
      let res = await getShopInfo();
      if (res.data.success) {
        this.storeData = res.data.result;
      }
    },
    handleClickMenu(val) {
      switch (val) {
        case "setup":
          uni.switchTab({
            url: `/pages/tabbar/user/my`,
          });
          break;
        case "logistics":
          uni.navigateTo({
            url: "/pages/other/setup/logistics",
          });
          break;
        case "stockWarning":
          uni.navigateTo({
            url: `/pages/other/setup/stockWarning?num=${this.storeData.stockWarning}`,
          });
          break;
        case "returnGoods":
          uni.navigateTo({
            url: `/pages/other/setup/returnGoodsAddress`,
          });
          break;
        // case "shopAddress":
        //   uni.navigateTo({
        //     url: `/pages/other/setup/shopAddressList`,
        //   });
        //   break;
        case "shopDetail":
          uni.navigateTo({
            url: `/pages/other/setup/shopDetail`,
          });
          break;
        case "upLoad":
          this.upLoad();
          break;
        case "cache":
          this.clearCache();
          break;
      }
    },
    // 获取应用版本
    getVersion() {
      plus.runtime.getProperty(plus.runtime.appid, (app) => {
        this.appVersion = app.version;
      });
    },
    // 获取应用的缓存大小
    getCache() {
      // #ifdef APP-PLUS
      plus.cache.calculate((size) => {
        if (size < 1024) {
          this.cacheSize = size + "B";
        } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
          this.cacheSize = Math.floor((size / 1024) * 100) / 100 + "KB";
        } else if (size / 1024 / 1024 >= 1) {
          this.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + "M";
        }
      });
      // #endif
      // #ifdef MP-WEIXIN
      let that = this;
      uni.getStorageInfo({
        success(res) {
          let size = res.currentSize;
          if (size < 1024) {
            that.cacheSize = size + " B";
          } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
            that.cacheSize = Math.floor((size / 1024) * 100) / 100 + "KB";
          } else if (size / 1024 / 1024 >= 1) {
            that.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + "M";
          }
        },
      });
      // #endif
    },
    // 清除当前设备缓存
    clearCache() {
      // #ifdef APP-PLUS
      let os = plus.os.name;
      if (os == "Android") {
        let main = plus.android.runtimeMainActivity();
        let sdRoot = main.getCacheDir();
        let files = plus.android.invoke(sdRoot, "listFiles");
        let len = files.length;
        for (let i = 0; i < len; i++) {
          let filePath = "" + files[i];
          plus.io.resolveLocalFileSystemURL(
            filePath,
            (entry) => {
              if (entry.isDirectory) {
                entry.removeRecursively(
                  (entry) => {
                    uni.showToast({
                      title: "清除成功",
                      icon: "none",
                    });
                    this.getCache();
                  },
                  (e) => {
                    console.log(e.message);
                  }
                );
              } else {
                entry.remove();
              }
            },
            (e) => {
              console.log("文件路径读取失败");
            }
          );
        }
      } else {
        plus.cache.clear(() => {
          uni.showToast({
            title: "清除成功",
            icon: "none",
          });
          this.getCache();
        });
      }
      // #endif
      // #ifdef MP-WEIXIN
      uni.clearStorageSync();
      uni.showToast({
        title: "清除成功",
        icon: "none",
      });
      this.getCache();
      // #endif
    },
    // 版本更新
    upLoad() {
      let parmas = {
        appType: plus.os.name == "Android" ? "ANDROID" : "IOS",
        userType: 3, // 1用户 2专家 3商家
      };
      getAppVersion(parmas).then((res) => {
        if (res.data.code == 200) {
          let info = res.data.result[0];
          plus.runtime.getProperty(plus.runtime.appid, (app) => {
            if (compareVersion(info.version, app.version) == 1) {
              uni.showModal({
                title: `检测到新版本${info.version}，是否更新?`,
                content: `更新内容：\n${info.content || "无"}`,
                success: (res) => {
                  if (res.confirm) {
                    var appurl = info.downloadUrl;
                    plus.runtime.openURL(appurl);
                  }
                },
              });
            } else {
              uni.showToast({
                title: "当前已是最新版本",
                icon: "none",
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
page {
  background-color: #ededed;
}

@media screen and(min-width:600px) {
  .ipad-title {
    font-size: 40rpx;
  }

  .ipad-dec {
    font-size: 32rpx;
  }
}

.camera {
  width: 54px;
  height: 44px;

  &:active {
    background-color: #ededed;
  }
}

.user-box {
  background-color: #fff;
}
</style>
